﻿
using CJGL;
using Comm;
using GYiwork.Devices.Printer;
using JShiwork.Devices.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net.Sockets;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml.Linq;
using Ztest;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;

namespace JShiwork.FrmUI
{
    public partial class frmMain : Form
    {
      

        public frmMain()
        {
            InitializeComponent();
        }


        private void frmMain_Resize(object sender, EventArgs e)
        {
            this.panel1.Width = this.Width / 2;
            this.panel1.Height = (this.Height / 2) - 500;
            textBox1.Location = new Point((this.Height / 4) - 10, 18); // 设置Label的Location属性
        }
        /// <summary>
        /// 加载主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
        
            ClsParmart.LoadPar();
            scanner1.finPrint += Scanner1_finPrint;

        }

        private void Scanner1_finPrint(string Mes)
        {
            Mes = Mes.TrimEnd('\r');
            if (Mes.Contains("NoRead")|| Mes.Contains("\r"))
            {
                ErrorCount++;
                this.Invoke(new Action(() =>
                {
                    this.label1.Text = ErrorCount.ToString();
                }));

                //输出报警
                Aelar("未采集到(No data collected)");
              

               
            }
            else
            {
                if (SqlDAL.GetCode(Mes))
                {
                    AddPrint(Mes);
                }
                else
                {
                    CFcount++;

                    this.Invoke(new Action(() =>
                    {
                        this.label2.Text = CFcount.ToString();
                    }));
                    Mes = "重复采集(Duplicate collection):" + Mes;
                    Aelar(Mes);
                }
            }

        }

        //剔除1
        private void tc1(object Mespar)
        {
           
                Thread.Sleep(int.Parse(ClsParmart.TCYS1));
                IOHelp.sendOpen(ClsParmart.alert_prot, 3);
                Thread.Sleep(int.Parse(ClsParmart.Alter_time1));
                IOHelp.sendClose(ClsParmart.alert_prot, 3);
            
        }

        //报警
        private void bj(object Mespar)
        {
          
                IOHelp.sendOpen(ClsParmart.alert_prot, 2);
                Thread.Sleep(int.Parse(ClsParmart.Alter_time3));
                IOHelp.sendClose(ClsParmart.alert_prot, 2);
          
        }

        private void Aelar(string Mes)
        {
           
            if (Mes.Contains("未采集到") || Mes.Contains("重复采集") || Mes.Contains("数量已经足够") || Mes.Contains("采集的数据错误"))
            {
                Thread th = new Thread(new ParameterizedThreadStart(tc1));
                th.Start(Mes);
            }
            insetLog(Mes);
            Thread th1 = new Thread(new ParameterizedThreadStart(bj));
            th1.Start(Mes);

        }


        private void AddPrint(string Mes)
        {
            PrintCount++;
            insetLog("采集成功(Scanner Successfully):" + Mes);
            SqlDAL.InsertCode(Mes);
            this.Invoke(new Action(() =>
            {
                // 向ListView添加数据
                this.lblCount.Text = PrintCount.ToString();
                textBox1.Text = Mes;
                ListViewItem item1 = new ListViewItem();
                item1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 
                item1.SubItems.Add(Mes);
               // item1.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                listView1.Items.Insert(0, item1);
            }));
           
        }



        /// <summary>
        /// 展示主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Shown(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 关闭主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            scanner1.Close();
            IOHelp.Close();
        }


        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void UiStatus(bool Enabled)
        {
            btnRun.Enabled = Enabled;
            btnStop.Enabled = !Enabled;
            mnuSystem.Enabled = Enabled;
            mnuSetup.Enabled = Enabled;
         
            btnClear.Enabled = Enabled;
            btnExit.Enabled = Enabled;
        

            if (Enabled)
            {
                this.tbShownPanel.Text = "系统停止中(Stopped)...";
                this.tbShownPanel.ForeColor = Color.Red;
            }
            else
            {
                this.tbShownPanel.Text = "运行中(Running)...";
                this.tbShownPanel.ForeColor = Color.Green;
            }
        }

        private void insetLog(string mes)
        {
            WriteFileCls.InserLog(mes);
            mes = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss：") + mes;

            this.Invoke(new Action(() =>
            {
                this.lb_RunMsg.Items.Insert(0, mes);
            }));
        }

        private void insetErr(string mes)
        {
            WriteFileCls.InserLog(mes);
            mes = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss：") + mes;

            this.Invoke(new Action(() =>
            {
                this.lb_ErrMsg.Items.Insert(0, mes);
            }));
        }


        TCPHelp1 scanner1 = new TCPHelp1();

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRun_Click(object sender, EventArgs e)
        {
            string isok = scanner1.Open(ClsParmart.scanner_ip1, ClsParmart.scanner_port1);
            if (isok != "ok")
            {
                insetLog("连接小码采集器失败:" + isok);
                btnRun.Enabled = true;
                UiStatus(true);
                return;
            }



            isok = IOHelp.Open(ClsParmart.alert_prot, ClsParmart.Baudrate);
            if (isok != "ok")
            {
                scanner1.Close();
                insetLog("连接IO盒子失败:" + isok);
                btnRun.Enabled = true;
                UiStatus(true);
                return;
            }

            UiStatus(false);

        

        }
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            scanner1.Close();
            IOHelp.Close();
            UiStatus(true);

        }
   
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            scanner1.Close();
            IOHelp.Close();
            this.Close();
        }
        /// <summary>
        /// 清屏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.lb_RunMsg.Items.Clear();
            this.lb_ErrMsg.Items.Clear();
            this.listView1.Items.Clear();
        }
      
     

        private void mnuSetup_Click(object sender, EventArgs e)
        {
            ConfigForm passfrom = new ConfigForm();


            if (passfrom.ShowDialog(this) == DialogResult.OK)
            {

            }
        }


        int PrintCount = 0;
        int ErrorCount = 0;
        int CFcount = 0;

        private void 授权registerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PassWord passfrom = new PassWord();
            if (passfrom.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new frmMain());
            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
        }
    }
}
